package com.example.Accounting14;

import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller // This means that this class is a Controller
@RequestMapping(path = "/Accounting14/cash") // This means URL's start with /demo (after Application path)
public class MainController {
	@Autowired // This means to get the bean called userRepository
	// Which is auto-generated by Spring, we will use it to handle the data
	private CashRepository cashRepository;
	@Autowired
	private AccountRepository accountRepository;

	@PostMapping(path = "/add") // Map ONLY POST Requests
	public String addNewUser(@RequestParam Integer costid, @RequestParam Integer taxid, @RequestParam Float fee) {

		Cash n = new Cash();
		n.setCostid(costid);
		n.setTaxid(taxid);
		n.setFee(fee);
		n.setStatus(0);
		cashRepository.save(n);
		return "redirect:/Accounting14/cash/newcash";

	}

	/* 定向到首页，显示整体框架，默认嵌套未审核数据 */
	@GetMapping("/index")
	public String index(Model model) {
		return "index";
	}

	/* 创建新缴费订单入口 */
	@GetMapping("/newcash")
	public String newcash(Model model) {
		return "cashedit_new";
	}

	/* 显示所有缴费订单，便于管理员修改 */
	@GetMapping("/all")
	public String showall(Model model) {
		Iterable<Cash> cashes = cashRepository.findAll();
		model.addAttribute("cashes", cashes);
		return "table_cash_admin";
	}

	/* 显示未审核的数据 */
	@GetMapping("/uncheck")
	public String uncheck(Model model) {
		Iterable<Cash> cashes = cashRepository.findByStatus(0);
		model.addAttribute("cashes", cashes);
		return "table_cash";
	}

	/* 显示未支付的数据 */
	@GetMapping("/unpaid")
	public String unpaid(Model model) {
		Iterable<Cash> cashes = cashRepository.findByStatus(1);
		model.addAttribute("cashes", cashes);
		return "table_cash";
	}

	/* 显示已支付的数据 */
	@GetMapping("/paid")
	public String paid(Model model) {
		Iterable<Cash> cashes = cashRepository.findByStatus(2);
		model.addAttribute("cashes", cashes);
		return "table_cash";
	}

	/* 回收站：显示被删除的数据 */
	@GetMapping("/trash")
	public String trash(Model model) {
		Iterable<Cash> cashes = cashRepository.findByStatus(4);
		model.addAttribute("cashes", cashes);
		return "table_cash";
	}

	/* 账户：显示全部银行账户 */
	@GetMapping("/account")
	public String allaccount(Model model) {
		Iterable<Account> accs = accountRepository.findAll();
		model.addAttribute("accs", accs);
		return "table_acc";
	}

	@PostMapping(path = "/editacc")
	public String editacc(@RequestParam String account, Model model) {
		Account accs = accountRepository.findByAccount(account);
		if (accs != null) {
			model.addAttribute("accs", accs);
			return "edit_acc";
		} else {
			return "false";
		}
	}

	/* 更新银行账户的余额、备注 */
	@PostMapping(path = "/updateacc")
	public String updateacc(@RequestParam String account, @RequestParam Float sum, @RequestParam String note,
			Model model) {
//		System.out.println("debug********************* print!!!!!!!!!!!!!");
		Account accs = accountRepository.findByAccount(account);
		if (accs != null) {
			accs.setSum(sum);
			accs.setNote(note);
			accountRepository.save(accs);
			return "redirect:/Accounting14/cash/account";
		} else {
			return "false";
		}
	}

	/* 转到编辑表单页面 Cash，针对不同状态的有不同的可编辑项 */
	@PostMapping(path = "/edit")
	public String edit(@RequestParam Integer id, @RequestParam Integer status, Model model) {
		Optional<Cash> cash = cashRepository.findById(id);
		model.addAttribute("cashes", cash.get());
		switch (status) {
		case 0:
			return "cashedit"; // 未审核订单的编辑
		case 1:
			return "cashedit_unpaid"; // 未支付订单的编辑
		case 2:
			return "cashedit_paid"; // 已支付订单的编辑
		case 3:
			return "cashedit_trash"; // 回收站订单的编辑
		case 4:
			return "cashedit_admin"; // 管理员：所有订单的编辑
		default:
			break;
		}
		return "cashedit";
	}

	/* 更新表单 cash */
	@PostMapping(path = "/update")
	public String update(@RequestParam Integer id, @RequestParam Integer status, @RequestParam Integer costid,
			@RequestParam Float cost, @RequestParam Integer taxid, @RequestParam Float tax, @RequestParam Float fee,
			@RequestParam Float money, @RequestParam String cashid, @RequestParam String account_from,
			@RequestParam String account_to, @RequestParam String staff, @RequestParam String note,
			@RequestParam(value = "admin", required = false, defaultValue = "0") Integer admin, Model model) {

		// 看起来是新建了一样，实则不是的，是根据 id 查找，然后更新，挺魔幻
		if (admin == 1) {

			Cash n = new Cash(id, status, costid, cost, taxid, tax, fee, money, cashid, account_from, account_to, note,
					staff);
			cashRepository.save(n); // 保存到数据库
		} else {
			if (status < 2) {
				if (status == 1) { // 确认支付时更新银行账户余额
					Account accs = accountRepository.findByAccount(account_to);
					accs.setSum(accs.getSum() + money);
					accountRepository.save(accs);
				}
				// 状态更新 + 1
				Cash n = new Cash(id, status + 1, costid, cost, taxid, tax, fee, money, cashid, account_from,
						account_to, note, staff);
				cashRepository.save(n); // 保存到数据库
			} else if (status == 2) {
				// 已支付状态不更新
				Cash n = new Cash(id, status, costid, cost, taxid, tax, fee, money, cashid, account_from, account_to,
						note, staff);
				cashRepository.save(n); // 保存到数据库
			}
		}

		// 页面重定向
		return "redirect:/Accounting14/cash/uncheck";
	}
}